using TMPro;
using UnityEngine;

public class PopUpFx : MonoBehaviour
{
    public TextMeshPro Tmp { get; private set; }
    [SerializeField] private float speed;
    [SerializeField] private float disappearSpeed;
    [SerializeField] private float colorDisappearSpeed;
    [SerializeField] private float lifeTime;
    private float textTimer;

    private void Awake()
    {
        Tmp = GetComponent<TextMeshPro>();
        textTimer = lifeTime;
    }

    private void Update()
    {
        transform.position = Vector2.MoveTowards(transform.position, new Vector2(transform.position.x, transform.position.y + 1), speed * Time.deltaTime);
        textTimer -= Time.deltaTime;
        if (textTimer < 0)
        {
            float alpha = Tmp.color.a - colorDisappearSpeed * Time.time;
            Color color = Tmp.color;
            Tmp.color = new Color(color.r, color.g, color.b, alpha);
            if (Tmp.color.a < 50)
            {
                speed = disappearSpeed;
            }
            if (Tmp.color.a <= 0)
                Destroy(gameObject);
        }
    }
}
